﻿using System.Linq;
using Foxit.PDF;
using Foxit.PDF.PageElements;
using System.Collections.Generic;
using Sustainalytics.Utils;

namespace Sustainalytics.Reporting.Framework
{
    public class PdfPageElement : IPdfPageElement
    {
        public string Name { get; set; }
        public Page Page { get; set; }
        public Group PageElements { get; set; }
        public float Left { get; set; }
        public float LeftOffset { get; set; }
        public float Top { get; set; }
        public int Row { get; set; }
        public int Column { get; set; }

        public void Draw()
        {
        }
    }

    public class PdfTable
    {
        protected float[] relativeWidths;
        protected float[] absoluteWidths;

        protected float totalWidth = 0.0f;
        protected float totalHeight = 0.0f;
        protected float widthPercentage = 80f;

        public int NumberOfColumns
        {
            get
            {
                return this.relativeWidths.Length;
            }
        }

        public float TotalHeight { get; set; }
        public float TotalWidth { get; set; }
        public float WidthPercentage { get; set; }

        public Page Page { get; set; }
        public float Left { get; set; }
        public float Top { get; set; }
        public float Width { get; set; }
        public float Height { get; set; }
        public Font TableFont { get; set; }
        public float TableFontSize { get; set; }
        public int Rows { get; set; }
        public int Columns { get; set; }
        public bool ShowGridLines { get; set; }

        public List<IPdfPageElement> Children { get; set; }

        public PdfTable()
        {
            Children = new List<IPdfPageElement>();
        }

        public void Draw()
        {
            if (Width.NearlyEq(0.0f))
                Width = Page.Dimensions.Body.Width - Left - 5f;

            if (Height.NearlyEq(0.0f))
                Height = Page.Dimensions.Body.Height - Top;

            var rowHeight = Height / Rows;
            var columnWidth = Width / Columns;

            var table = new Table(Left, Top, Width, Height, TableFont, TableFontSize);
            table.Border.Width = 0.0f;
            table.Border.Color = Grayscale.Black;
            table.Border.LineStyle = LineStyle.None;

            table.CellSpacing = 0.0f;
            table.CellDefault.Align = TextAlign.Left;
            table.CellDefault.VAlign = VAlign.Top;
            table.CellDefault.Padding.Value = 0.0f;
            table.CellDefault.Border.Color = RgbColor.Blue;
            table.CellDefault.Border.Width = 0.0f;
            table.CellDefault.Border.LineStyle = LineStyle.None;

            table.RepeatColumnHeaderCount = 1;
            table.RepeatRowHeaderCount = 0;

            for (var column = 1; column <= Columns; column++)
            {
                table.Columns.Add(columnWidth);
            }

            for (var row = 1; row <= Rows; row++)
            {
                var tableRow = table.Rows.Add(rowHeight);
                tableRow.CellDefault.Align = TextAlign.Center;
                tableRow.CellDefault.VAlign = VAlign.Center;

                for (var column = 1; column <= Columns; column++)
                {
                    if (ShowGridLines)
                    {
                        tableRow.Cells.Add(
                            new Rectangle(0.0f, 0.0f, columnWidth, rowHeight, RgbColor.Blue, RgbColor.Aquamarine, 0.5f, LineStyle.Dash));
                    }

                    var pageElements = Children.Where(c => c.Row == row
                                                        && c.Column == column)
                                           .Select(c => c.PageElements)
                                           .ToList();

                    foreach (var pageElement in pageElements)
                    {
                        tableRow.Cells.Add(pageElement);
                    }
                }
            }

            Page.Elements.Add(table);
        }
    }

}
